﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Users;
using ShopSpace.Core;

namespace ShopSpace.UI.DotNetNukeModules
{
	public partial class ViewProjectPersons : EditModuleControlBase
	{

		private Project project;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			try
			{
				this.InitParameters();
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				this.SetupLinks();
				if ( !this.Page.IsPostBack )
				{
					this.BindProject();
					this.BindGrid();
				}
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		protected void gvPersons_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if ( e.Row.RowType == DataControlRowType.DataRow )
			{
				ProjectPerson projectPerson = (ProjectPerson) e.Row.DataItem;
				Label lblName = (Label) e.Row.FindControl("lblName");
				lblName.Text = UserController.GetUserByName(this.PortalId, projectPerson.Person.Username, true).DisplayName;

				Label lblRole = (Label) e.Row.FindControl("lblRole");
				lblRole.Text = projectPerson.Role.ToString();

				HyperLink hlEdit = (HyperLink) e.Row.FindControl("hlEdit");
				hlEdit.NavigateUrl = EditUrl("Id", projectPerson.Id.ToString(), "EditProjectPerson", "ProjectId=" + this.project.Id.ToString());

				LinkButton lnkDelete = (LinkButton) e.Row.FindControl("lnkDelete");
				lnkDelete.CommandName = "DeleteProjectPerson";
				lnkDelete.CommandArgument = e.Row.RowIndex.ToString();
				lnkDelete.Attributes.Add("onclick", "return confirm('Are you sure you want to remove this person from the project?');");
			}
		}

		protected void gvPersons_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			try
			{
				if ( e.CommandName == "DeleteProjectPerson" )
				{
					int rowIndex = Convert.ToInt32(e.CommandArgument);
					int projectPersonId = Convert.ToInt32(this.gvPersons.DataKeys[rowIndex].Value);
					ProjectPerson toRemove = this.ProjectPersonRepository.Get(projectPersonId);
					this.ProjectPersonRepository.Delete(toRemove, this.UserInfo.Username);
					Response.Redirect(Globals.NavigateURL());
				}
			}
			catch ( Exception ex )
			{
				this.HandleException(ex);
			}
		}

		private void InitParameters()
		{
			this.project = this.ProjectRepository.Get(this.editItemId.Value);
		}

		private void BindProject()
		{
			this.lblProject.Text = string.Format("People in {0}", this.project.Name);
		}

		private void SetupLinks()
		{
			this.hlAdd.NavigateUrl = EditUrl("ProjectId", this.project.Id.ToString(), "EditProjectPerson");
			this.hlCancel.NavigateUrl = Globals.NavigateURL();
		}

		private void BindGrid()
		{
			this.gvPersons.DataKeyNames = new string[] { "Id" };
			this.gvPersons.DataSource = this.ProjectPersonRepository.ListByProject(project);
			this.gvPersons.DataBind();
		}
	}
}